package com.imooc.springboot.es.controller;

import com.imooc.springboot.es.entity.es.EsBlog;
import com.imooc.springboot.es.entity.mysql.MysqlBlog;
import com.imooc.springboot.es.repository.es.EsBlogRepository;
import com.imooc.springboot.es.repository.mysql.MysqlBlogRepository;
import lombok.Data;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
public class DataController {

    @Autowired
    private MysqlBlogRepository mysqlBlogRepository;

    @Autowired
    private EsBlogRepository esBlogRepository;

    @GetMapping("/blogs")
    public Object blog(){
        List<MysqlBlog> mysqlBlogs = mysqlBlogRepository.queryAll();
        return mysqlBlogs;
    }


    @PostMapping("/search")
    public Object search(@RequestBody Param param){
        Map<String,Object> map = new HashMap<>();
        StopWatch watch = new StopWatch();
        watch.start();
        String type = param.getType();
        if (type.equalsIgnoreCase("mysql")){
            List<MysqlBlog> mysqlBlogs = mysqlBlogRepository.queryBlogs(param.getKeyword());
            map.put("list",mysqlBlogs);
        }else if (type.equalsIgnoreCase("es")){
            /**
             * POST blog/_search
             * {
             *   "query": {
             *     "bool": {
             *       "should": [
             *         {
             *           "match_phrase": {
             *             "title": "springBoot"
             *           }
             *         },
             *         {
             *           "match_phrase": {
             *             "content": "springBoot"
             *           }
             *         }
             *       ]
             *     }
             *   }
             * }
             */
            BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
            queryBuilder.should(QueryBuilders.matchPhraseQuery("title",param.getKeyword()));
            queryBuilder.should(QueryBuilders.matchPhraseQuery("content",param.getKeyword()));

            String s = queryBuilder.toString();
            System.out.println(s);

            Page<EsBlog> search = (Page<EsBlog>) esBlogRepository.search(queryBuilder);

            List<EsBlog> content = search.getContent();
            map.put("list",content);
        }else {
            return "I don't understand!";
        }
        watch.stop();

        map.put("duration",watch.getTotalTimeMillis());
        return map;
    }

    @GetMapping("/blog/{id}")
    public Object blog(@PathVariable("id") Integer id){
        Optional<MysqlBlog> optional = mysqlBlogRepository.findById(id);
        MysqlBlog blog = optional.get();
        return blog;
    }


    @Data
    private static class Param{
        // mysql,es
        private String type;
        // 搜索关键字
        private String keyword;
    }
}
